package com.mycompany.virtualApp.controller;

import com.mycompany.mvc.utils.BaseResult;
import com.mycompany.mvc.utils.SystemUtil;
import com.mycompany.virtualApp.entity.User;
import com.mycompany.virtualApp.service.TopService;
import com.mycompany.virtualApp.service.UserService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局的接口
 */

@Controller
//@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    /**
     * 登录
     */
    @RequestMapping("/login")
    @ResponseBody
    public ModelAndView login(@RequestParam(value = "username", required = false) String name,
                              @RequestParam(value = "password", required = false) String password,
                              HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();

        if(StringUtils.isAnyEmpty(name,password)){
            //回到当前页面
            mv.setViewName("system/login");
            return mv;
        }

        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("password",password);
        map.put("isDelete",0);
        User user = userService.selectOneByParam(map);
        if(SystemUtil.isNotNull(user)){
            mv.addObject("name",user.getName());
            mv.addObject("realName",user.getRealName());

            mv.addObject(user);
            mv.setViewName("system/main");
            return mv;
        }

        return mv;
    }

    /**
     * 登录
     */
    @RequestMapping("/testLogin")
    @ResponseBody
    public BaseResult testLogin(@RequestParam(value = "username", required = false) String name,
                            @RequestParam(value = "password", required = false) String password,
                            HttpServletRequest request) {
        BaseResult baseResult = new BaseResult();

        if(StringUtils.isAnyEmpty(name,password)){
            baseResult.setSuccess(false);
            return baseResult;
        }

        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("password",password);
        map.put("isDelete",0);
        User user = userService.selectOneByParam(map);
        if(SystemUtil.isNotNull(user)){
            baseResult.setSuccess(true);
            return baseResult;
        }

        return  baseResult;
    }

}
